//
//  LCTransmissionListVC.m
//  LongChain
//
//  Created by FengZi on 2018/4/9.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCTransmissionListVC.h"
#import "LCTransmissionListCell.h"
typedef NS_ENUM(NSInteger, TransferType) {
    TransferTypeUpload,
    TransferTypeDownload
};
@interface LCTransmissionListVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UIButton *delButton;

@property (weak, nonatomic) IBOutlet UIView *withOutDataBaseView;
@property (weak, nonatomic) IBOutlet UIImageView *withOutDataImage;
@property (weak, nonatomic) IBOutlet UILabel *withOutDataDes;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *delButtonConstraint;
@property (weak, nonatomic) IBOutlet UICollectionView *uploadAndDownloadList;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *shortLinePosition;

@property (nonatomic, strong) NSMutableArray *selectedDatas;
@property (nonatomic, strong) NSMutableArray *uploadOrDownloadDatas;

@property (nonatomic, assign) TransferType transferType;


@end

@implementation LCTransmissionListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self localDataConfig];
    
    self.transferType = TransferTypeUpload;
}

- (void)localDataConfig {
    
    self.delButtonConstraint.constant = 0.0f;
    self.selectedDatas = [NSMutableArray arrayWithCapacity:0];
    NSArray *temp = @[@"0",@"1",@"2",@"3",@"5",@"4",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15"];
    self.uploadOrDownloadDatas = [NSMutableArray arrayWithArray:temp];
}

- (void)updateWithoutDataDes:(TransferType)type {
    
    switch (self.transferType) {
        case TransferTypeUpload:  //  上传
        {
            self.withOutDataImage.image = [UIImage imageNamed:@"transferUp"];
            self.withOutDataDes.text = @"这里是空的,赶紧去上传吧";
        }
            break;
        case TransferTypeDownload:  //  下载
        {
            self.withOutDataImage.image = [UIImage imageNamed:@"transferDown"];
            self.withOutDataDes.text = @"这里是空的,赶紧去下载吧";

        }
            break;
            
        default:
            break;
    }
    
}
#pragma mark - - UICollectionViewDataSource UICollectionViewDelegate
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"transmissionHeader" forIndexPath:indexPath];
        return header;
    }
    return nil;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LCTransmissionListCell *transmissionListCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"transmissionCell" forIndexPath:indexPath];
    return transmissionListCell;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.uploadOrDownloadDatas.count;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    return CGSizeMake(SWidth, 50);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SWidth, 60);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    LCLog(@"点击");
    LCTransmissionListCell *transmissionListCell = (LCTransmissionListCell *)[collectionView cellForItemAtIndexPath:indexPath];
    if ([self.selectedDatas containsObject:@(indexPath.row)]) {
        transmissionListCell.transmissStatus.image = [UIImage imageNamed:@"list_ic_circle_n"];
        [self.selectedDatas removeObject:@(indexPath.row)];


    }else{
        transmissionListCell.transmissStatus.image = [UIImage imageNamed:@"list_ic_circle_h"];
        [self.selectedDatas addObject:@(indexPath.row)];

    }
    self.delButtonConstraint.constant = (self.selectedDatas.count > 0) ? 44.0f : 0.0f;
}


#pragma mark - -

- (IBAction)delFiles:(id)sender {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"确定删除文件" message:@"确定删除后文件不可恢复" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self.selectedDatas enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSInteger row = [obj integerValue];
            
            [self.uploadOrDownloadDatas removeObjectAtIndex:row];
        }];
        [self.selectedDatas removeAllObjects];
        [self.uploadAndDownloadList reloadData];
    }];
    
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:confirmAction];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}
- (IBAction)chooseFileAction:(id)sender {
    
    LCLog(@"选择");
}

- (IBAction)chouseTransferType:(UIButton *)sender {
    
    self.shortLinePosition.constant = (sender.center.x) - (SWidth / 4);

    switch (sender.tag) {
        case 100:
        {
            self.transferType = TransferTypeUpload;

        }
            break;
        case 200:
        {
            self.transferType = TransferTypeDownload;

        }
            break;
            
        default:
            break;
    }
    [self updateWithoutDataDes:self.transferType];

    
}


- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
